<template>
  <el-card shadow="hover">
    <el-form>
      <el-form-item>
        <el-input placeholder="用户名" class="user-inp" />
        <el-button type="primary" :icon="Search" class="ml-20">
          查询
        </el-button>
        <el-button> 清空 </el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="mt-20" @click="addRole">
          添 加
        </el-button>
        <el-button type="danger" class="mt-20"> 批量删除 </el-button>
      </el-form-item>
      <el-table :data="roleList" border v-loading="loading">
        <el-table-column type="selection" />
        <el-table-column label="序号" type="index" align="center" width="80" />
        <el-table-column label="角色名称" prop="roleName" />
        <el-table-column label="操作" width="500">
          <template v-slot="{ row }">
            <el-button
              type="info"
              :icon="InfoFilled"
              size="small"
              title="分配权限"
            />
            <el-button type="primary" :icon="Check" size="small" title="确定" />
            <el-button
              type="primary"
              :icon="Edit"
              size="small"
              title="修改角色"
            />
            <el-button
              type="danger"
              :icon="Delete"
              size="small"
              title="删除角色"
            />
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="mt-20"
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[3, 6, 9, 12]"
        layout="prev, pager, next, jumper, ->, sizes, total"
        :total="total"
        @size-change="getRoleList"
        @current-change="getRoleList"
      />
    </el-form>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "CUser",
};
</script>

<script lang="ts" setup>
import { getRoleApi, addRoleListApi } from "@/api/acl/role";
import type { RoleList } from "@/api/acl/model/roleModel";
import {
  Search,
  Edit,
  Delete,
  InfoFilled,
  Check,
} from "@element-plus/icons-vue";
import { onMounted, ref } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";

// 当前页码
const currentPage = ref<number>(1);
// 每页条数
const pageSize = ref<number>(5);
// 总页数
const total = ref<number>(0);
// 定义loading效果
const loading = ref(false);

const roleList = ref<RoleList>([]);

const getRoleList = async () => {
  loading.value = true;
  const res = await getRoleApi(currentPage.value, pageSize.value);
  roleList.value = res.items;
  total.value = res.total;
  loading.value = false;
};

onMounted(getRoleList);

const addRole = () => {
  ElMessageBox.prompt("请输入新名称", "添加角色", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    inputErrorMessage: "请输入合法的名称",
  })
    .then(async ({ value }) => {
      // console.log(value);

      await addRoleListApi({
        roleName: value,
      });
      ElMessage({
        type: "success",
        message: "添加成功",
      });
      getRoleList();
    })
    .catch(() => {
      ElMessage({
        type: "warning",
        message: "取消添加",
      });
    });
};
</script>

<style scoped>
.user-inp {
  width: 200px;
}
</style>
